package com.atg.atgchat.init.user.model.vo.adapter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.atg.atgchat.init.user.model.entity.ItemConfig;
import com.atg.atgchat.init.user.model.entity.User;
import com.atg.atgchat.init.user.model.entity.UserBackpack;
import com.atg.atgchat.init.user.model.enums.YesOrNoEnum;
import com.atg.atgchat.init.user.model.vo.BadgeVo;
import com.atg.atgchat.init.user.model.vo.UserInfoVo;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/*
author: atg
time: 2025/1/16 15:56
desc: 返回前端数据适配
*/
public class UserAdapter {

    public static UserInfoVo buildUserInfoVo(User byId, Integer countModifyName) {
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(byId, userInfoVo);
        userInfoVo.setModifyNameChance(countModifyName);
        return userInfoVo;
    }

    public static List<BadgeVo> buildBadgeVo(List<ItemConfig> itemConfigs, List<UserBackpack> daoByItemIds, User user) {
        if (ObjectUtil.isNull(user)) {
            return Collections.emptyList();
        }
        Set<Long> collect = daoByItemIds.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());
        return itemConfigs.stream().map(itemConfig -> {
            BadgeVo badgeVo = new BadgeVo();
            BeanUtil.copyProperties(itemConfig, badgeVo);
            badgeVo.setObtain(collect.contains(itemConfig.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            badgeVo.setWearing(ObjectUtil.equal(itemConfig.getId(), user.getItemId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
            return badgeVo;
        }).sorted(Comparator.comparing(BadgeVo::getWearing, Comparator.reverseOrder())
                .thenComparing(BadgeVo::getObtain, Comparator.reverseOrder())).collect(Collectors.toList());
    }
}
